var { Client, Message } = require('./utils/paho-mqtt.js')

// app.js
async function wxLogin(){
  return new Promise(function(reslove,reject){
    wx.login({
      success (res) {
        reslove(res.code);
      }
    })
  })
}
App({

  globalData: {
    userInfo: null,
    //urlGlobal:'http://119.45.93.149/',
    urlGlobal:'https://www.zcxbb.com/',
    appid:'wx2d050228ef6b9746',//appid需自己提供，此处的appid我随机编写
    secret:'ed3d47950d69250fa50b0c9773847c1f',//secret需自己提供，此处的secret我随机编写
    //client : new Client("wss://www.zcxbb.com/mqtt", randomString())
    client : new Client("wss://www.zcxbb.com:61615/", randomString()),
    mqttIfConnetc:false,
    mqttConnetc:(gl)=>{
      if(!gl.mqttIfConnetc){//gl globalData
        gl.client.connect({
          useSSL: true,
          cleanSession: true,
          keepAliveInterval: 5,
          reconnect:true,
          onSuccess: function () {
            gl.mqttIfConnetc=true
          }
        });
      }
    }
  },

  onLaunch:function (){

    let that = this


    console.log("app.onLaunch")
    console.log(that.globalData)



      // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })


    that.globalData.mqttConnetc(that.globalData)//gl globalData






  },




})
function randomString(len) {
  len = len || 32;
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  var maxPos = $chars.length;
  var pwd = '';
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}